package com.huniu.links.colony;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;

import com.huniu.links.exception.LinkProcessException;
import com.huniu.links.model.ModelXMLProperty;

public abstract class AbstractBeeBehaviorImpl implements IBeeBehavior {

	public Document parseXMLFile(File file) throws DocumentException,
			LinkProcessException {
		SAXReader reader = new SAXReader();
		Document document = reader.read(file);
		generateProcessedFile(file);
		return document;
	}

	public void generateProcessedFile(File file) {
		String processedFileName = file.getAbsolutePath()
				+ ModelXMLProperty.PROCESSED_EXTENSION;
		File processedFile = new File(processedFileName);
		file.renameTo(processedFile);
	}

	@SuppressWarnings("unchecked")
	public List<String> parseLinks(Document document, String elementTag) {
		org.dom4j.Element root = document.getRootElement();
		Iterator<org.dom4j.Element> iterator = root.elementIterator(elementTag);
		List<String> urlList = new ArrayList<String>();
		while (iterator.hasNext()) {
			org.dom4j.Element element = iterator.next();
			String url = element.getText();
			urlList.add(url);
		}
		return urlList;
	}

	public void process() {

	}

	public void run() {

	}

}
